﻿<cfcomponent extends="com.cargopooling.Object">

	<cffunction name="init" access="public" returntype="XMLColdSpringLoader">
		<cfreturn this/>
	</cffunction>
	
	<cffunction name="load" access="public" returntype="void" output="false" hint="Load plugin into the framework.">
		<cfargument name="name" type="String" required="true" hint="Plugin name" />
		<cfargument name="path" type="String" required="true" hint="ColdSpring XML file path" />
		<cfargument name="cargoPooling" type="com.cargopooling.apollo.CargoPooling" required="true" />
		<cfset var plugin = "" />
		<cfset var serviceFactory = createServiceFactory( xmlParse( arguments.path ) ) />
		
		<cfif serviceFactory.containsBean( 'plugin' )>
		
			<cfset arguments.cargoPooling.addPlugin( arguments.name, serviceFactory.getBean('plugin') ) />
			
		<cfelse>
		
			<cfset Exception( 'Error to add plugin to core. Bean named PLUGIN not found.' ) />
		
		</cfif>
		
	</cffunction>
	
	<!--- coldspring loader utility --->

	<cffunction name="createServiceFactory" access="private" output="false" >
		<cfargument name="serviceDefinition" type="xml" required="true" />
		<cfargument name="defaultProperties" type="struct" required="false" default="#structNew()#"/>
		<cfset var serviceFactory = createObject( 'component' , 'coldspring.beans.DefaultXmlBeanFactory' ).init( structNew(), arguments.defaultProperties ) />
		<cfset serviceFactory.loadBeansFromXmlObj( arguments.serviceDefinition ) />
		<cfreturn serviceFactory />
	</cffunction>

</cfcomponent>